/*

    This file is part of Hometree.

    Hometree is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Hometree is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Hometree.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2011,2012 Roman Held

*/

/********************************************************************\
                          Includes
\********************************************************************/
#include <QDateTime>
#include <QDebug>
#include <QImage>
#include "htr/htrmimedatavariantadapter.h"

/********************************************************************\
                       Template-Funktionen
\********************************************************************/
template <class enumType>
QVariant fromType(const HtrMimeDataAdapter *a) 
{
 QDataStream stream(a->mimeData(), QIODevice::ReadOnly);
 enumType val;
 stream >> val;
 return val;
}

/********************************************************************\
                        Implementation
\********************************************************************/
/*--------------------------------------------------------------------
*/
HtrMimeDataVariantAdapter::HtrMimeDataVariantAdapter(QObject *parent)
 : HtrMimeDataAdapter(parent)
{
  mMimeTypes.insert("DataStream/Int32"   ,DataStreamInt32);
  mMimeTypes.insert("DataStream/Int64"   ,DataStreamInt64);
  mMimeTypes.insert("DataStream/Double"  ,DataStreamDouble);
  mMimeTypes.insert("DataStream/String"  ,DataStreamString);
  mMimeTypes.insert("DataStream/DateTime",DataStreamDateTime);
  mMimeTypes.insert("DataStream/Image"   ,DataStreamImage);
  mMimeTypes.insert("text/plain"         ,PlainText);
  mMimeTypes.insert("text/html"          ,PlainText);
  mMimeTypes.insert("QImage"             ,Image);
}

/*--------------------------------------------------------------------
*/
HtrMimeDataVariantAdapter::~HtrMimeDataVariantAdapter()
{
}

/*--------------------------------------------------------------------
*/
void HtrMimeDataVariantAdapter::init(QByteArray *data, const QString &type)
{
  Q_ASSERT(data);
  HtrMimeDataAdapter::init(data,type);
  mMimeType = Invalid;
  if (mMimeTypes.contains(type))
    mMimeType = (MimeType)mMimeTypes[type];
  else
    qWarning() << "HtrMimeDataVariantAdapter::init: no converter for type:" << type;
}

/*--------------------------------------------------------------------
*/
bool HtrMimeDataVariantAdapter::isValid() const
{
  return mMimeType != Invalid;
}

/*--------------------------------------------------------------------
*/
QVariant HtrMimeDataVariantAdapter::value() const
{
  QVariant ret;
  switch (mMimeType) {
    case Invalid : break;
    case DataStreamInt32 : ret = fromType<qint32>(this); break;
    case DataStreamInt64 : ret = fromType<qint64>(this); break;
    case DataStreamDouble: ret = fromType<double>(this); break;
    case DataStreamBoolean:ret = fromType<bool>(this); break;
    case DataStreamString: ret = fromType<QString>(this); break;
    case DataStreamDateTime:ret= fromType<QDateTime>(this); break;
    case DataStreamImage : ret = fromType<QImage>(this); break;
    case PlainText       : ret = fromPlainText(); break;
    case Image           : ret = fromImage(); break;
  }
  return ret;
}

/*--------------------------------------------------------------------
*/
QList<QString> HtrMimeDataVariantAdapter::supportedMimeTypes() const
{
  return mMimeTypes.keys();
}

/*--------------------------------------------------------------------
*/
QVariant HtrMimeDataVariantAdapter::fromPlainText() const
{
  Q_ASSERT(mimeData());
  return QString(mimeData()->data());
}

/*--------------------------------------------------------------------
*/
QVariant HtrMimeDataVariantAdapter::fromImage() const
{
  Q_ASSERT(mimeData());
  return QImage::fromData(*mimeData());
}

